﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zmc;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Customers;

namespace MyMES.Writer.API.Application.CommendHandlers.Customers
{
    public class UpdataCustomersCommendHandlers : IRequestHandler<UpdataCustomersCommends, APIResult<Customermanagement>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRepository<Customermanagement> customerrepository;

        public UpdataCustomersCommendHandlers(IMapper mapper, IBaseRepository<Customermanagement> customerrepository)
        {
            this.mapper = mapper;
            this.customerrepository = customerrepository;
        }


        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Customermanagement>> Handle(UpdataCustomersCommends request, CancellationToken cancellationToken)
        {
            APIResult<Customermanagement> result = new APIResult<Customermanagement>();
            result.Code = APIEnum.修改成功;
            result.Msg = "修改成功";
            var customer = mapper.Map<Customermanagement>(request);
            string sql = "UPDATE customermanagement SET CustomerNo=@CustomerNo,CustomerName=@CustomerName,Customerabbrevias=@Customerabbrevias,CustomerEng=@CustomerEng,CustomerTypeId=@CustomerTypeId,Customerdescris=@Customerdescris,CustomerAddress=@CustomerAddress,Customeroffadd=@Customeroffadd,Customeremadd=@Customeremadd,Customerphone=@Customerphone,Customerlogo=@Customerlogo,Contactone=@Contactone,Contactphoneone=@Contactphoneone,Contactemailone=@Contactemailone,Contacttwo=@Contacttwo,Contactphonetwo=@Contactphonetwo,Contactemailtwo=@Contactemailtwo,SocialCredit=@SocialCredit,Customerremark=@Customerremark,Isok=@Isok where CustomerId=@CustomerId";
            await customerrepository.Update(customer, sql);
            return result;
        }
    }
}
